<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" pageEncoding="UTF-8" %>
<script>
    $(document).ready(function () {
        $.post("${pageContext.request.contextPath}/role/findAllRole", (result) => {
            $.each(result.roles, function (i, r) {
                $("#addRole").append("<label><input type='checkbox' value='" + r.id + "' name='checkbox' title='" + r.roleName + "'>&nbsp;" + r.roleName + "&nbsp;&nbsp;</label>")
            })
        }, "JSON");
        $("#up").click(function () {
            let username = $("#username").val();
            let password = $("#password").val();
            let name = $("#name").val();
            let age = $("#age").val();
            let sex = $("#sex").find('option:selected').val();
            let ids = [];
            $("input:checkbox:checked").each(function (i) {
                ids[i] = $(this).val();
            });
            $.ajax({
                type: 'post',
                traditional: true,
                url: "${pageContext.request.contextPath}/user/addUser",
                data: {"username": username, "password": password, "name": name, "age": age, "sex": sex, "ids": ids},
                success: function (result) {
                    window.location.href = `javascript:$('#content').load('./user/userManager.jsp')`;
                }
            })
        });
    });


</script>
<div class="col-sm-10" style="align-content: center ">
    <div class="page-header">
        <h1>管理中心</h1>
    </div>
    <ul class="nav nav-tabs">
        <li class="active"><span>添加用户</span></li>
    </ul>
    <form>
        <div id="userPager">
            <div class="form-group">
                <label for="username" class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="username" name="username" placeholder="用户名">
                </div>
            </div>
            <br/><br/><br/>
            <div class="form-group">
                <label for="username" class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="password" name="password" placeholder="密码">
                </div>
            </div>
            <br/><br/>
            <div class="form-group">
                <label for="name" class="col-sm-2 control-label">姓名</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="name" name="name" placeholder="姓名">
                </div>
            </div>
            <br/><br/>
            <div class="form-group">
                <label for="age" class="col-sm-2 control-label">年龄</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="age" name="age" placeholder="年龄">
                </div>
            </div>
            <br/><br/>
            <div class="form-group">
                <label for="sex" class="col-sm-2 control-label">性别</label>
                <div class="col-sm-10">
                    <select class="form-control" name="sex" id="sex">
                        <option value="男">男</option>
                        <option value="女">女</option>
                    </select>
                </div>
            </div>
            <br/><br/>
            <div class="form-group">
                <label for="addRole" class="col-sm-2 control-label">添加角色</label>
                <div class="col-sm-10" id="addRole">
                </div>
            </div>
            <br/><br/>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="button" class="btn btn-default" id="up">提交</button>
                </div>
            </div>
        </div>
    </form>
</div>

